package org.rslai.tcedit.business.impl;

import java.io.IOException;

import javax.activation.DataHandler;
import javax.jms.*;

import org.rslai.tcedit.business.TestScriptService;
import org.rslai.tcedit.jms.JmsService;
import org.rslai.tcedit.jms.service.UnZip;
import org.rslai.tcedit.jms.service.RunTestCase;
import org.rslai.tcedit.web.service.RunConf;
import org.rslai.tcedit.web.service.TestCaseInfo;
import org.rslai.tcedit.web.service.TestScriptInfo;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;

/**
 * TestScriptService 接口的实现<br/>
 * 
 * @author rslai
 * 
 * @spring.bean id="testScriptService"
 */
public class TestScriptImpl extends AbstractBaseService implements TestScriptService {
	private JmsTemplate jmsTemplate;
	private Destination destination;

	/**
	 * @spring.property ref="jmsConnectionFactory"
	 */
	public void setConnectionFactory(ConnectionFactory cf) {
		this.jmsTemplate = new JmsTemplate(cf);
	}

	/**
	 * @spring.property ref="jmsDestination"
	 */
	public void setDestination(Destination destination) {
		this.destination = destination;
	}

	/**
	 * 发送 jms 消息
	 * @param jmsService 需要发送的 jms 服务
	 */
	private void sendMessage(final JmsService jmsService) {
		this.jmsTemplate.send(this.destination, new MessageCreator() {
			public Message createMessage(Session session) throws JMSException {
				return session.createObjectMessage(jmsService);
			}
		});
	}

	@Override
	public void uploadTestScript(TestScriptInfo testScriptInfo, DataHandler scriptData) throws IOException {
		// 1、保存上传的zip文件

		// 解压缩脚本文件
		this.sendMessage((JmsService)new UnZip("zipFileName", "destDir")); // 发送 jms 消息
	}

	@Override
	public void runTestCase(RunConf runConf, TestCaseInfo testCaseInfo, String testCaseStr) {
		this.sendMessage((JmsService)new RunTestCase(runConf, testCaseInfo, testCaseStr)); // 发送 jms 消息
	}

}
